home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_03 / v7n3010a.txt < prev    next >
Text File  |  1989-01-14  |  14KB  |  529 lines

  1.   
  2. Listing 1:
  3.  
  4. int well_formed(), well_formed_2(), woof();    /* Declarations */
  5.  
  6. typedef struct element {
  7.    char *field;
  8.    int (*validate)();
  9.    } FORM_RULES;
  10.  
  11. FORM_RULES form[] ={
  12.    { "field1", well_formed},
  13.    { "field2", well_formed_2}
  14.    };
  15.  
  16. int well_formed()
  17.    {
  18.    return 0;
  19.    }
  20.  
  21. int well_formed_2()
  22.    {
  23.    return 0;
  24.    }
  25.  
  26. int woof()
  27.    {
  28.    return 0;
  29.    }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. Listing 2:
  36.  
  37. main()
  38. /* Changes well_formed reference to woof */
  39.    {
  40.    int i;    
  41.    printf("\n Function addresses are %lx %lx %lx",
  42.        well_formed, well_formed_2, woof);
  43.  
  44.    printf("\n Member addresses are %lx %lx",
  45.        form[0].validate, form[1].validate);
  46.    
  47.    form[0].validate = woof;
  48.  
  49.    printf("\n Member addresses are %lx %lx",
  50.        form[0].validate, form[1].validate);
  51.  
  52.    }
  53.  
  54. Output:
  55.  
  56.  Function addresses are 3e220000 3e220009 3e220012
  57.  Member addresses are 3e220000 3e220009
  58.  Member addresses are 3e220012 3e220009
  59.  
  60.  
  61.  
  62. Listing 3:
  63.  
  64. struct element
  65.    {
  66.    char *field;
  67.    int validate_function_id;
  68.    };
  69.  
  70. #define F_WELL_FORMED 0
  71. #define F_WELL_FORMED_2 1
  72. #define F_WOOF 2
  73.  
  74. struct element form[] = {
  75.    { "field1", F_WELL_FORMED},
  76.    { "field2", F_WELL_FORMED_2},
  77.    };
  78.  
  79. int well_formed(), well_formed_2(), woof();    /* Declarations */
  80.  
  81. static int (*validate_function[])() =  
  82.                                 { well_formed, well_formed_2, woof};
  83. #define MAX_VALIDATE_FUNC  (sizeof(validate_function)/sizeof(int (*)()) )
  84.  
  85. main()
  86.    {
  87.    form[0].validate_function_id = F_WOOF;   
  88.    
  89.    /* Indirect call */
  90.    call_function(form[0].validate_function_id);
  91.  
  92.    /* Direct call -- if no checking was desired */    
  93.    (*validate_function[form[0].validate_function_id])();   
  94.    }
  95.  
  96. call_function(id)
  97. int id;
  98.     {
  99.    /* Check the function id and call it */
  100.    if (id >= 0 && id < MAX_VALIDATE_FUNC) 
  101.       (*validate_function[id])();
  102.    else
  103.        printf("\n Out of range validate function id", id);
  104.     }
  105.  
  106.  
  107.  
  108. Listing 4:
  109.  
  110. typedef int (* VALIDATER)();    /* Pointer to C function returning int */    
  111. #define VNOP ((VALIDATER) 0 )   /* Null function -- validate not required */
  112.  
  113. typedef struct field_element {
  114.    char **fptr;        /* Address of a field pointer */
  115.    VALIDATER fi;       /* Pointer to field function,
  116.                           if fi == NOP then no field is not touched */
  117.    } FORM_RULES;
  118.  
  119. /* Pointers to allocated fields */
  120.  
  121. char *Field_A, *Field_B;
  122.  
  123. /* Form definition */
  124.  
  125. FORM_RULES twx[] = {
  126.    { &Field_A, VNOP},
  127.    { &Field_B, VNOP},
  128.    };
  129.  
  130. main()
  131.    {
  132.    Field_A = "ABC";    /* Same as malloc, but with content */
  133.    Field_B = "DEF";    
  134.  
  135.    printf("\n Field A via pointer is %s double pointer %s",
  136.        Field_A, *(twx[0].fptr));
  137.    printf("\n Field B via pointer is %s double pointer %s",
  138.        Field_B, *(twx[1].fptr));
  139.    }
  140.  
  141.  
  142.  
  143.  
  144. Listing 5:
  145.  
  146. #include <stdio.h>
  147.  
  148. typedef int VALIDATE;
  149. typedef VALIDATE (* VALIDATER)();    /* Pointer to C function returning int */    
  150.  
  151. #define VNOP ((VALIDATER) 0 )   /* Null function -- validate not required */
  152.  
  153. typedef struct field_element {
  154.    char **fptr;        /* Address of a field pointer */
  155.    VALIDATER *fi;       /* Pointer to field function,
  156.                           if fi == NOP then no field is not touched */
  157.    } FORM_RULES;
  158.  
  159. /* Pointers to functions */
  160.  
  161. VALIDATER Function_A;
  162. VALIDATER Function_B;
  163.  
  164. /* Form definition */
  165.  
  166. FORM_RULES twx[] = {
  167.    { NULL, &Function_A},
  168.    { NULL, &Function_B},
  169.    };
  170.  
  171. VALIDATE function_one(), function_two();
  172.  
  173. main()
  174.    {
  175.    Function_A = function_one;
  176.    Function_B = function_two;
  177.    
  178.    /* Call the functions */
  179.    (**twx[0].fi)();
  180.    (**twx[1].fi)();   
  181.    }
  182.  
  183. VALIDATE function_one()
  184.     {
  185.     printf("\n Function one called");
  186.     return 0;
  187.     }
  188.  
  189. VALIDATE function_two()
  190.     {
  191.     printf("\n Function two called");
  192.     return 0;
  193.     }
  194.  
  195.  
  196. Listing 6:
  197.  
  198. typedef int VALIDATE;
  199. #define VNOP ((VALIDATE *) 0 )   /* Null function -- validate not required */
  200.  
  201. struct field_element {
  202.    char **fptr;        /* Address of a field pointer */
  203.    VALIDATE **fi;      /* Pointer to pointer to field function */
  204.    }; 
  205.  
  206. /* Pointers to functions */
  207.  
  208. VALIDATE *Function_A;
  209. VALIDATE *Function_B;
  210.  
  211. struct field_element twx[] = {
  212.    { NULL, &Function_A},
  213.    { NULL, &Function_B},
  214.    };
  215.    
  216.  
  217.  
  218.  
  219.     func_a()
  220.        {
  221.        printf("\n Func_a was called--need the real one for this \n");
  222.        return ;
  223.        }
  224.  
  225.  
  226.  
  227.  
  228.  
  229. SCREEN.C Listing 
  230.  
  231. /*
  232. *       SCREEN.C
  233. *  Modification of IVCLOCK using intervention code
  234. *
  235. *  SCREEN goes to the scheduler via timer every 37 clock tics
  236. *  The timer clock ticks every 55 msec. x 37 == once per 2 seconds
  237. *  OR every keyboard entry.
  238. *
  239. *  The command line format is as follows:
  240. *
  241. *       screen
  242. *
  243. *       screen -r
  244. *                  removes the system from resident status
  245. *
  246. *  A Change had to be made in the Interrupt Handler for INT 9
  247. *    to allow the system to respond on every key stroke
  248. *
  249. *  CTL-F1  puts the system to sleep (without removing it)
  250. *  CTL-F2  wakes it up again.
  251. *
  252. *                                       David Tal -8/XI/88
  253. */
  254.  
  255.  
  256. #include <dos.h>
  257. #include <stdio.h>
  258. #include <binterv.h>
  259. #include <bintrupt.h>
  260. #include <bkeys.h>
  261. #include <bscreens.h>
  262.  
  263.                 /* scr is the function which will be called        */
  264.                 /* approximately once per 2 sec. or at a keystroke */
  265. extern void scr (IV_EVENT *);
  266.  
  267. #define STKSIZE  2000
  268. #define OK       0
  269. #define FALL_OUT 101
  270.  
  271. #define NOT_FOUND         1
  272. #define ERROR_DISABLING   2
  273. #define ERROR_REMOVING    3
  274. #define ALREADY_INSTALLED 4
  275.  
  276.              /* Definitive signature for this version of sched.*/
  277. #define ol_sign "screen 08/11/88"
  278.  
  279.                 /* Allocation of stack space for the intervention   */
  280.                 /* scheduler and user function.                     */
  281. char schedstk [STKSIZE];
  282.  
  283.                 /* This is the data structure to pass to the        */
  284.                 /* scheduler so that it will give control every     */
  285.                 /* 37 clock ticks.                                     */
  286. IV_TIME timetab = {37, IV_TM_INTERVAL};
  287.  
  288. IV_KEY keytab[3] = {
  289.                       KB_C_C_F3,KB_S_C_F3,IV_KY_SERVICE,
  290.                       KB_C_C_F1,KB_S_C_F1,IV_KY_SLEEP,
  291.                       KB_C_C_F2,KB_S_C_F2,IV_KY_WAKE
  292.                    };
  293.                 /* Internal functions -- install & remove ISR.      */
  294. int install_iv (void);
  295. int remove_iv  (void);
  296.  
  297.  
  298.  /* here is the main program   */
  299.  
  300. int main (argc, argv)
  301. int   argc;
  302. char *argv [];
  303. {
  304.     if (argc == 1)
  305.         exit (install_iv ());
  306.  
  307.     if ((argc == 2)                                      &&
  308.         (((argv [1][0] == '-') || (argv [1][0] == '/')) &&
  309.          ((argv [1][1] == 'r') || (argv [1][1] == 'R'))))
  310.         exit (remove_iv ());
  311.     printf ("usage: screen [-r]\n");
  312.     exit (0);
  313. }
  314.  
  315. /**
  316. *
  317. * Name          INSTALL_IV -- Install interrupt vectors for IVREADAD and
  318. *                             go TSR(terminate and stay resident).
  319. *
  320. * Synopsis      ret = install_iv ();
  321. *
  322. *               int ret         Return code from IVINSTAL if there was
  323. *                               a problem with installation of the
  324. *                               intervention code routine.
  325. *
  326. * Description   This function installs screen if another copy
  327. *               is not already installed.
  328. *
  329. * Returns       ret     ALREADY_INSTALLED (4)
  330. *                           A copy of screen is already installed.
  331. *                       FALL_OUT (101)
  332. *                           ISRESEXT() failed.
  333. *                       IV_INSTALLED (5)
  334. *                           A copy of screen is already installed.
  335. *
  336. **/
  337.  
  338. #include <butil.h>
  339.  
  340. int install_iv ()
  341.  
  342. {
  343.     int          ercode;
  344.     IV_VECTORS   vecs;
  345.  
  346.                 /* Check to see if IVREADAD already installed.      */
  347.     ivvecs (IV_RETVEC, &vecs);
  348.     if (ivsense (&vecs, ol_sign) != FARNIL)
  349.     {
  350.         puts ("SCREEN already installed.");
  351.         return (ALREADY_INSTALLED);
  352.     }
  353.                 /* Install the interrupt service routine--i.e. tell */
  354.                 /* the scheduler a